﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using IFServerDLL.Model;

namespace IFServerDLL.Broker
{
    [DataContract]
    public class UserOrderTrade : BrokerUserBase
    {
        public UserOrderTrade(BrokerUserBase user)
        {
            var infos = typeof(BrokerUserBase).GetProperties().Where(p => p.CanWrite);
            foreach (var x in infos)
            {
                x.SetValue(this, x.GetValue(user, null), null);
            }
            Account = new AccountBase(1000000);
            TradeHistory = new List<TradeBase>();
            OrderHistory = new List<OrderBase>();
            CashRatio = new List<MyCashRatio>();
            CommissRatio = new List<MyCommissRatio>();
            ListTradeOnhand = new List<TradeOnHand>();
            ListTradeGroup = new List<TradeGroupBase>();
            foreach (var s in user.MarketInstrument)
            {
                MyCashRatio c = new MyCashRatio();
                c.Instrument = s;
                c.CashRatio = GlobalStatic.GetCashRatio(s);
                CashRatio.Add(c);
                MyCommissRatio com = new MyCommissRatio();
                com.Instrument = s;
                com.CommissRatio = GlobalStatic.GetCommissRatio(s); 
                CommissRatio.Add(com);
            }
            Account.Update(this);

        }
        [DataMember]
        public AccountBase Account { get; set; }
        [DataMember]
        public List<TradeBase> TradeHistory { get; set; }
        [DataMember]
        public List<MyCashRatio> CashRatio { get; set; }
        [DataMember]
        public List<MyCommissRatio> CommissRatio { get; set; }
        [DataMember]
        public List<OrderBase> OrderHistory { get; set; }
        
        
        public List<OrderBase> GetOrderOnhand()
        {
            return OrderHistory.Where(p => p.OrderStatusType == EnumOrderStatusType.已触发 || p.OrderStatusType == EnumOrderStatusType.未成交还在队列中 || p.OrderStatusType == EnumOrderStatusType.部分成交还在队列中).ToList();
        }
        public List<TradeGroupBase> ListTradeGroup { get; set; }
        public List<TradeOnHand> ListTradeOnhand { get; set; }
        
        public string AddTrade(TradeBase trade)
        {
            TradeHistory.Add(trade);
            if (trade.ALLVolumne > 0)//open
            {
                var onhand = ListTradeOnhand.Where(p => p.Instrument == trade.Instrument && p.BuyOrSell == trade.BuyOrSell).FirstOrDefault();
                if (onhand == null)
                {
                    onhand = new TradeOnHand(trade);
                    ListTradeOnhand.Add(onhand);
                }
                else
                {
                    onhand.UpdateTrade(trade);

                }
            }
            else
            {
                var onhand = ListTradeOnhand.Where(p => p.Instrument == trade.Instrument && p.BuyOrSell != trade.BuyOrSell).FirstOrDefault();
                if (onhand != null)
                {
                    onhand.UpdateTrade(trade);
                    if (onhand.SumALLVolume == 0)
                    {
                        var tradeGroup = ListTradeGroup.Where(p => p.Instrument == trade.Instrument && p.BuyOrSell != trade.BuyOrSell).FirstOrDefault();
                        if (tradeGroup == null)
                        {
                            tradeGroup = new TradeGroupBase(onhand);
                            ListTradeGroup.Add(tradeGroup);
                        }
                        else
                        {
                            tradeGroup.AddTradeOnhand(onhand);
                        }
                        ListTradeOnhand.Remove(onhand);
                    }
                }
                else
                {
                    return ("no hand to close.");
                }
            }
            return string.Empty;
            
        }




    }
}
